<template>
  <div style="margin-top: 16px">
    <el-form label-width="100px" size="small" >
      <el-form-item label="脚本格式">
        <el-input v-model="scriptTaskForm.scriptFormat" clearable @input="updateElementTask()" @change="updateElementTask()" />
      </el-form-item>
      <el-form-item label="脚本类型">
        <el-select v-model="scriptTaskForm.scriptType">
          <el-option label="内联脚本" value="inline" />
          <el-option label="外部资源" value="external" />
        </el-select>
      </el-form-item>
      <el-form-item label="脚本" v-show="scriptTaskForm.scriptType === 'inline'">
        <el-input
          v-model="scriptTaskForm.script"
          type="textarea"
          resize="vertical"
          :autosize="{ minRows: 2, maxRows: 4 }"
          clearable
          @input="updateElementTask()"
          @change="updateElementTask()"
        />
      </el-form-item>
      <el-form-item label="资源地址" v-show="scriptTaskForm.scriptType === 'external'">
        <el-input v-model="scriptTaskForm.resource" clearable @input="updateElementTask()" @change="updateElementTask()" />
      </el-form-item>
      <el-form-item>
        <template slot="label">
            <span>
               结果变量
               <el-tooltip placement="top">
                  <template slot="content">
                     <div>
                        服务执行的返回值（仅对使用表达式的服务任务），可以通过为服务任务定义的'flowable:resultVariable'属性设置为流程变量。
                       <br />可以是已经存在的，或者新的流程变量。 如果指定为已存在的流程变量，则流程变量的值会被服务执行的结果值覆盖。
                       <br />如果不指定结果变量名，则服务任务的结果值将被忽略。
                     </div>
                  </template>
                  <i class="el-icon-question" />
               </el-tooltip>
            </span>
        </template>
        <el-input v-model="scriptTaskForm.resultVariable" clearable @input="updateElementTask()" @change="updateElementTask()" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "ScriptTaskPanel",
  props: {
    id: {
      type: String,
      required: true
    },
  },
  data() {
    return {
      defaultTaskForm: {
        scriptFormat: "",
        script: "",
        resource: "",
        resultVariable: ""
      },
      scriptTaskForm: {}
    };
  },
  watch: {
    id: {
      immediate: true,
      handler() {
        this.bpmnElement = this.modelerStore.element;
        this.$nextTick(() => this.resetTaskForm());
      }
    }
  },
  methods: {
    resetTaskForm() {
      for (let key in this.defaultTaskForm) {
        let value = this.bpmnElement?.businessObject[key] || this.defaultTaskForm[key];
        this.$set(this.scriptTaskForm, key, value);
      }
      this.$set(this.scriptTaskForm, "scriptType", this.scriptTaskForm.script ? "inline" : "external");
      this.deleteElementFormExtensions();
    },
    updateElementTask() {
      let taskAttr = Object.create(null);
      taskAttr.scriptFormat = this.scriptTaskForm.scriptFormat || null;
      taskAttr.resultVariable = this.scriptTaskForm.resultVariable || null;
      if (this.scriptTaskForm.scriptType === "inline") {
        taskAttr.script = this.scriptTaskForm.script || null;
        taskAttr.resource = null;
      } else {
        taskAttr.resource = this.scriptTaskForm.resource || null;
        taskAttr.script = null;
      }
      this.modelerStore.modeling.updateProperties(this.bpmnElement, taskAttr);
    },
    // 新建节点时,默认是用户节点,会添加表单相关扩展属性,当前任务直接删除掉
    deleteElementFormExtensions() {
      const element = this.modelerStore.element;
      if (element.businessObject?.extensionElements?.values) {
        element.businessObject.extensionElements.values =
          element.businessObject.extensionElements.values.filter(ex => ex.$type !== 'flowable:FormItems');
      }
    }
  },
  beforeDestroy() {
    this.bpmnElement = null;
  }
};
</script>
